function required(message: string = "此项必填", trigger: string = "blur") {
  //必填
  return {
    required: true,
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/^\s*$/g);
      if (
        reg.test(value) ||
        value === null ||
        value === undefined ||
        value === ""
      ) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger,
    message,
  };
}

function number(message: string = "只能输入数字") {
  //数字
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value === "") {
        callback(undefined);
      }
      const reg = new RegExp(/^[0-9]+$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function space(message: string = "不能为空") {
  //不能为空
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value.trim() === "") {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function minNumber(message: string = "只能输入数字且大小在1-19之间") {
  //数字且大小在1-19
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value === "") {
        callback(undefined);
      }
      const reg = new RegExp(/^([1-9]|1\d)$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function double(message: string = "只能是数字") {
  //浮点型数字
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/[0-9]+$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function numberLim(message: string = "数字限制最大长度") {
  //数字限制最大长度
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/^[0-9]{0,5}$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function string(message: string = "只能输入英文或数字") {
  //只能输入英文
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/^[a-zA-Z0-9]+$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function NumAndStr(message: string = "只能是小写字母和数字") {
  //只能小写字母或者数字
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/^[a-z0-9]+$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function checkChinese(message: string = "不能含有汉字") {
  // 中文数据验证 --ASCII码
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      let flag = true;
      if (value && value != null) {
        for (let i = 0; i < value.length; i++) {
          if (value.charCodeAt(i) > 255) {
            callback(new Error(message));
            flag = false;
            break;
          }
        }
      }
      if (flag) {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function isCardNo(message: string = "身份证格式不正确") {
  //身份证校验
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function email(message: string = "请输入正确的邮箱地址") {
  //email
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value === "") {
        callback(undefined);
      }
      const reg = new RegExp(
        /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
      );
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "change",
  };
}

function mobile(message: string = "请输入正确的手机号") {
  //移动手机号
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/^1\d{10}$/);
      if (!reg.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function tel(message: string = "请输入正确的固定电话") {
  //固定电话
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value === "") {
        callback(undefined);
      } else {
        const reg = new RegExp(/^0\d{2,3}-?\d{7,8}$/);
        if (!reg.test(value)) {
          callback(new Error(message));
        } else {
          callback(undefined);
        }
      }
    },
    trigger: "blur",
  };
}

function emailOrMobile(message: string = "请输入正确的邮箱地址或手机号码") {
  // email or mobile
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value === "") {
        callback(undefined);
      }
      const regEmail = new RegExp(
        /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
      );
      const regMobole = new RegExp(/^1\d{10}$/);

      if (regEmail.test(value) && regMobole.test(value)) {
        callback(undefined);
      } else {
        callback(new Error(message));
      }
    },
    trigger: "change",
  };
}

function mobileOrTel(message: string = "请输入正确的邮箱地址或手机号码") {
  // mobile or tel
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      if (value === "") {
        callback(undefined);
      }
      const regMobole = new RegExp(/^1\d{10}$/);
      const regTel = new RegExp(/^0\d{2,3}-?\d{7,8}$/);

      if (regMobole.test(value) && regTel.test(value)) {
        callback(undefined);
      } else {
        callback(new Error(message));
      }
    },
    trigger: "change",
  };
}

function verificationCode(message: string = "请输入6位数字验证码") {
  //6位手机验证码
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp(/\d{6}$/);
      if (!reg.test(value)) {
        return callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function isURL(message: string = "URL格式不正确") {
  // URL格式验证
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      const reg = new RegExp("^(http://|https://|/).*$");
      if (!reg.test(value) && value.trim() != "") {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
  };
}

function pswd(message: string = "您输入的密码不满足要求") {
  //密码校验，及复杂度校验
  let grade;
  return {
    validator: (
      _rule: any,
      value: string,
      callback: (arg0: Error | undefined) => void
    ) => {
      // 密码中必须包含大小字母、数字、特称字符，至少8个字符，最多16个字符。
      const reg1 = new RegExp(
        "(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,16}"
      );
      // 密码中必须包含字母、数字、特称字符，至少8个字符，最多16个字符。
      const reg2 = new RegExp(
        "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}"
      );
      // 密码中必须包含字母、数字，至少8个字符，最多16个字符。
      const reg3 = new RegExp("(?=.*[0-9])(?=.*[a-zA-Z]).{8,16}");

      if (reg1.test(value)) {
        grade = 1;
      } else if (reg2.test(value)) {
        grade = 2;
      } else if (reg3.test(value)) {
        grade = 3;
      } else {
        grade = 0;
      }
      if (value === "" || value === undefined) {
        callback(undefined);
      } else if (!reg3.test(value)) {
        callback(new Error(message));
      } else {
        callback(undefined);
      }
    },
    trigger: "blur",
    grade: grade,
  };
}

export default {
  required,
  number,
  space,
  minNumber,
  double,
  numberLim,
  string,
  NumAndStr,
  isCardNo,
  email,
  mobile,
  tel,
  emailOrMobile,
  mobileOrTel,
  verificationCode,
  checkChinese,
  isURL,
  pswd,
};
